/**
 * 
 */
package com.suixun.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.entity.BatchInfo;
import com.suixun.entity.form.ParamForm;
import com.suixun.service.BatchService;
import com.suixun.util.Constant;
import com.suixun.vo.BatchVO;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/batchManage")
public class BatchManage extends BaseManager {
	@Resource
	private BatchService batchService;

	/**
	 * 查询所有的批号列表
	 * 
	 * @return
	 */

	@RequestMapping(value = "queryBatch.json")
	@ResponseBody
	public Object queryBatchList(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			param = "";
		}
		try {
			ParamForm paramForm = new Gson().fromJson(param, ParamForm.class);
			return success(batchService.queryBatchlist(paramForm));
		} catch (Exception e) {
			logger("BatchManage queryBatchList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}
	
	
	
	/**
	 * 新增批号
	 * 
	 * @return
	 */

	@RequestMapping(value = "addBatch.json")
	@ResponseBody
	public Object addBatch(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			ParamForm paramForm = new Gson().fromJson(param, ParamForm.class);
			if (paramForm == null || StringUtil.isBlank(paramForm.getBatchid())) {
				return fail(Constant.EMPTY);
			}
			//先判断批号名称是否已存在
			List<BatchInfo> batchList=batchService.queryBatchByCode(paramForm);
			if(batchList.size()>0) {
				return fail(Constant.Batch.BATCH_IS_EXIST);
			}
			int res = batchService.addBatch(paramForm);
			if(res>0)
				return success(res);
			else
				return fail(Constant.OPERATE_FAIL);
			
		} catch (Exception e) {
			logger("BatchManage addBatch:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}

	/**
	 * 修改批号
	 * 
	 * @return
	 */

	@RequestMapping(value = "modifyBatch.json")
	@ResponseBody
	public Object modifyBatch(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			ParamForm paramForm = new Gson().fromJson(param, ParamForm.class);
			if (paramForm == null || StringUtil.isBlank(paramForm.getBatchid()) || paramForm.getId()==null) {
				return fail(Constant.EMPTY);
			}
			//先判断批号名称是否已存在
			List<BatchInfo> batchList=batchService.queryBatchByCode(paramForm);
			if(batchList.size()>0) {
				return fail(Constant.Batch.BATCH_IS_EXIST);
			}
			int res = batchService.modifyBatch(paramForm);
			if(res>0) {
				return success(res);
			}else {
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("BatchManage modifyBatch:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}
	
	/**
	 * 删除批号
	 * 
	 * @return
	 */

	@RequestMapping(value = "deleteBatch.json")
	@ResponseBody
	public Object deleteBatch(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			ParamForm paramForm = new Gson().fromJson(param, ParamForm.class);
			if (paramForm == null || StringUtil.isBlank(paramForm.getId())) {
				return fail(Constant.EMPTY);
			}
			BatchInfo batch=batchService.queryBatchByID(paramForm);
			if(batch==null)
				return fail(Constant.Batch.BATCH_IS_NOT_EXIST);
			paramForm.setBatchid(batch.getBatchcode());
			//先判断批号是不是还有未删除的设备，如果有，不能删除
			int studentsCount=batchService.queryBatchDeviceCounts(paramForm);
			if(studentsCount!=0) {
				return fail(Constant.Batch.DEVICE_IS_EXIST);
			}
			int res = batchService.delBatch(batch.getId());
			
			if(res>0) {
				return success(res);
			}else {
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("BatchManage deleteBatch:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}
	
	/**
	 * 导出excel，不做鉴权
	 * 
	 * @return
	 */

	@RequestMapping(value = "export_BatchList.json")
	@ResponseBody
	public Object exportBatchList(String param) {
		boolean bln = checkRight();
		if (!bln) {
			toRedirect(404);
		}
		if (param == null)
			param="";
		try {
			ParamForm paramForm = new Gson().fromJson(param, ParamForm.class);
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add(Constant.Device.BATCHID);
			
			List<List<String>> listValue=new LinkedList<List<String>>();
			//限制最多导出1万条数据，前端可以通过细化查询条件来多次导出
			if(StringUtil.isBlank(paramForm.getPagecount()))
				paramForm.setPagecount(Constant.Excel.EXPORT_ROWS);
			List<BatchVO> list = batchService.queryBatchlist(paramForm);

			for (int m = 0; m < list.size(); m++) {
				List<String> value=new LinkedList<String>();
				BatchVO batch = list.get(m);
				value.add(batch.getBatchid());
				listValue.add(value);
				batch = null;
			}
			
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList(Constant.Excel.BATCH_EXCEL,listParm,listValue);
			HttpServletResponse response=this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=batchlist.xls");
			workbook.write(this.getResponse().getOutputStream());

		} catch (Exception e) {
			logger("BatchManage exportBatchList:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
}
